package com.softa.web.controller.business;

import com.github.pagehelper.PageInfo;
import com.softa.annotation.CurrentUser;
import com.softa.dao.model.ProductType;
import com.softa.dao.model.SysUser;
import com.softa.service.business.ProductTypeService;
import com.softa.vo.DataGridRequest;
import com.softa.vo.DataGridResponse;
import com.softa.vo.QueryRequest;
import com.softa.web.controller.base.BaseController;
import com.softa.utils.Constants;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by yangl on 2017/7/30.
 * 产品类型
 */
@Controller
@RequestMapping("/producttype")
public class ProductTypeController  extends BaseController {
    @Resource
    private ProductTypeService productTypeService;

    @RequestMapping(method = RequestMethod.GET)
    public String init(Model model) {
        return Constants.MENU_MANAGE+"/product/type/list";
    }

    @RequiresPermissions("producttype:view")
    @RequestMapping(value="/listPage",method = RequestMethod.POST)
    @ResponseBody
    public DataGridResponse listPage(DataGridRequest request, QueryRequest queryRequest) {
        PageInfo<ProductType> pageResult = productTypeService.queryProductTypeByParam(request, queryRequest);
        return new DataGridResponse(pageResult);
    }

    @RequiresPermissions("producttype:view")
    @RequestMapping(value="/showaddpage",method = RequestMethod.GET)
    public String addTypepage(String param, Model model) {
        return Constants.MENU_MANAGE+"/product/type/add_type";
    }

    @RequiresPermissions("producttype:view")
    @RequestMapping(value="/edittypepage",method = RequestMethod.GET)
    public String edituserpage() {
        return Constants.MENU_MANAGE+"/product/type/add_type";
    }

    @RequiresPermissions("producttype:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public String create(@CurrentUser SysUser loginUser, ProductType productType) {
        String message="录入成功";
        try {
            productType.setCreateUser(loginUser.getId());
            productType.setUpdateUser(loginUser.getId());
            message=productTypeService.createProductType(productType);
        } catch (Exception e) {
            message="录入失败";
            logger.error(e.getMessage());
        }
        return message;
    }

    @RequiresPermissions("producttype:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@CurrentUser SysUser loginUser, ProductType productType) {
        String message="修改成功";
        try {
            productType.setUpdateUser(loginUser.getId());
            message=productTypeService.updateType(productType);
        } catch (Exception e) {
            message="修改失败";
            logger.error(e.getMessage());
        }
        return message;
    }

    @RequiresPermissions("producttype:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@PathVariable("id") Long id) {
        String message="删除成功";
        try {
            productTypeService.deleteType(id);
        } catch (Exception e) {
            message="删除失败";
            logger.error(e.getMessage());
        }
        return message;
    }
}
